from django.shortcuts import render_to_response
from django.http import Http404, HttpResponse
from django import template
from project.coms.models import Computer

#--------------------------- index ---------------------------
def index(request):
    return render_to_response('index.html')

#--------------------------- search_form ---------------------------
def search_form(request):
    return render_to_response('search_form.html')#10

#--------------------------- sendrequest ---------------------------
def sendrequest(request):
    return render_to_response('sendrequest.html')

#--------------------------- search ---------------------------
def search(request):
    errors = []
    if 'q' in request.GET:
        q = request.GET['q']
        if not q:
            errors.append('Please enter a computer name.')#20
	elif len(q) > 7:
            errors.append('Please enter at most 7 characters.')
        else:									
            coms = Computer.objects.filter(comp_ID__icontains=q)
            return render_to_response('search_results.html',
                {'coms': coms, 'query': q})
    return render_to_response('search_form.html',{'errors': errors})

#--------------------------- showcomputername ---------------------------
def showcomputername(request):
    showname = Computer.objects.all()#30
    return render_to_response('showcomputername.html',{'showname':showname})

#--------------------------- send ---------------------------
def send(request):
    errors = []
    if 'cid' in request.GET:
        cid = request.GET['cid']
	cstatus = request.GET['cstatus']
        if not cid:
            errors.append('Please enter a computer name.')
	elif len(cid) > 7:#40
            errors.append('Please enter a computer name at most 7 characters.')

	if not cstatus:
	    errors.append('Please enter a computer status.')
	elif len(cstatus) > 20:
	    errors.append('Please enter a computer name at most 20 characters.')	

        else:
		try:	    
            		temp = Computer.objects.get(comp_ID = '%s'%cid) #50
	    		temp.comp_Status = '%s'%cstatus
   	    		temp.save()
			showname = Computer.objects.all()
    	    		return render_to_response('showcomputername.html',{'showname':showname})
		except Computer.DoesNotExist:
			errors.append('no this Computer name in Database.')
	    

    return render_to_response('sendrequest.html',{'errors': errors})
#60

#--------------------------- addcomputer ---------------------------
def addcomputer(request):
    return render_to_response('addcomputer.html')

#--------------------------- addfunc ---------------------------
def addfunc(request):
    errors = []
    if 'cid' in request.GET:
        cid = request.GET['cid']
        if not cid:
            errors.append('Please enter a computer name.')
	elif len(cid) > 7:#70
            errors.append('Please enter a computer name at most 7 characters.')

        else:
		try:	
			if Computer.objects.get(comp_ID = '%s'%cid):
			    errors.append('It is already have this Computer in Database.')    
            	except Computer.DoesNotExist:	
			    temp = Computer.objects.create(comp_ID = '%s'%cid) 
	    		    temp.comp_Status = 'online'				
   	    		    temp.save()
			    showname = Computer.objects.all()#30
    	    		    return render_to_response('showcomputername.html',{'showname':showname})
    return render_to_response('addcomputer.html',{'errors': errors})

#--------------------------- fixer ---------------------------
def fixer(request):
    showname = Computer.objects.all()
    return render_to_response('fixer.html',{'showname':showname})

#--------------------------- fixer ---------------------------
def fixed(request):
    showname = Computer.objects.all()
    errors = []
    if 'cid' in request.GET:
        cid = request.GET['cid']
        if not cid:
            errors.append('Please enter a computer name.')
	elif len(cid) > 7:#70
            errors.append('Please enter a computer name at most 7 characters.')
	else:
		try:
	    	        Computer.objects.get(comp_ID = '%s'%cid,comp_Status = 'online')
			errors.append('It still can use.')
		except Computer.DoesNotExist:
            		temp = Computer.objects.get(comp_ID = '%s'%cid)
	    		temp.comp_Status = 'online'
   	    		temp.save()
    	    		return render_to_response('fixer.html',{'showname':showname})
    return render_to_response('fixer.html',{'errors': errors,'showname':showname})



















